package com.hg.miaosha.common.exception;


import com.hg.miaosha.common.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理器
 * Created by skh on 2017/12/22
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

	/**
	 * 处理全局异常
	 *
	 */
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public R handleException(Exception e) {
		log.error(e.getMessage(), e);
		return R.error(e.getMessage());
	}

	/**
	 * 处理运行时异常
	 *
	 */
	@ExceptionHandler(RuntimeException.class)
	@ResponseBody
	public R handleRuntimeException(RuntimeException e) {
		log.error(e.getMessage(), e);
		return R.error(e.getMessage());
	}
	
	/**
	 * 处理自定义异常
	 *
	 */
	@ExceptionHandler(BizException.class)
	@ResponseBody
	public R handleBizException(BizException e) {
		log.error(e.getMessage(), e);
		return R.error(e.getCode(), e.getMessage());
	}

	/**
	 * 处理参数检查异常
	 *
	 */
	@ExceptionHandler(ParamCheckException.class)
	@ResponseBody
	public R handleParamCheckException(ParamCheckException e) {
		log.error(e.getMessage(), e);
		return R.error(e.getCode(), e.getMessage());
	}
}